import * as tRoomService from '../../services/tRoom';
export default {
  namespace: 'tRoom',
  state: {
    dataCenterList: [],
    tRoomList: [],
    current : 1,
    currentSize : 10,
    total : 0, 
    datacenterId : null,  
  },
  reducers: {
    
    queryDataCenterSuc(state, { payload: dataCenterList }) {
      return { ...state, dataCenterList };
    },
    queryTRoomSuc(state, { payload }) {
      return { ...state, ...payload };
    },
  },
  effects: {    
    *queryDataCenter({ payload },{ call, put }) {
      const { body } = yield call(tRoomService.queryDataCenter,payload);
      yield put({ type: 'queryDataCenterSuc', payload: body.data });
    }, 
    *queryTRoom({ payload },{ call, put }) {
      const { body } = yield call(tRoomService.queryList,payload);
      yield put({ type: 'queryTRoomSuc', payload: { tRoomList:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords, datacenterId:payload.datacenterId }});
    },
    *create({ payload ,search},{ call, put }){
      const { body } = yield call(tRoomService.create,payload);
       yield put({ type: 'queryTRoom', payload:search});
    },
    *edit({ payload,search },{ call, put }){
      const { body } = yield call(tRoomService.edit,payload);
      yield put({ type: 'queryTRoom', payload:search});
    },
    *remove({ payload ,search},{ call, put }){
      const { body } = yield call(tRoomService.remove,payload);
      yield put({ type: 'queryTRoom', payload:search});
    },
   
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/basic/tRoom') {
          dispatch({ type: 'queryDataCenter'});
          dispatch({ type: 'queryTRoom', payload:{ 'datacenterId':'','name':'' }});
        }
      });
    },
  },
};
